/*
 * Worker.h
 *
 *      Author: Jerome RD Soine and Christoph A Brand
 *      Institution: Schwarz goup, Institute for Theoretical Physics, Heidelberg University, Germany
 */

#ifndef WORKER_H_
#define WORKER_H_

#include <vector>
#include "../CellModel.h"
#include "../SubstrateModel.h"
#include "../Comparator.h"
#include <boost/thread.hpp>
#include <boost/date_time.hpp>

class Worker {
private:
	CellModel * cell_model;
	SubstrateModel * substrate_model;
	Comparator * comparator;
	int id;
	bool locked;
	boost::thread t;
	double fitness;
	double joinable;

public:
	Worker(CellModel * CELL_MODEL, SubstrateModel * SUBSTRATE_MODEL, Comparator * COMPERATOR, int ID);
	~Worker();

	void init(vector<double> parameters, int ID);
	void init(vector<double> parameters, vector<double> init_config, int ID);
	void work();
	double get_fitness();
	vector<double> get_final_configuration(){ return cell_model->get_final_configuration(); };

	int get_id(){return id;};
	void set_id(int i){id=i;};

	CellModel * get_cell_model() { return cell_model; };
	SubstrateModel * get_substrate_model() { return substrate_model; };
	Comparator * get_comparator() { return comparator; };

	//THREAD MANAGEMENT
	void start();
	void join();
	bool try_join_for(long millisecs);
	void interrupt(){t.interrupt();}
	bool isLocked(){return locked;};
	void lock(){locked=true;};
	void unlock(){locked=false;};
	bool is_joinable(){return joinable;};
};


#endif /* WORKER_H_ */
